package com.hsh.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hsh.bean.*;
import com.hsh.service.AdminService;
import com.hsh.service.CommentService;
import com.hsh.service.UserService;
import com.hsh.util.ToBootStrapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private UserService userService;

    @Resource
    private CommentService commentService;

    /**
     * 管理员登录登录
     * @param admin
     * @param model
     * @return
     */
    @RequestMapping("/login")
    public String login(Admin admin, Model model){
        //根据账户名和密码查询
        List<Admin> login = adminService.login(admin);

        Message msg = new Message();

        //验证成功，跳转到管理员页面
        if(login.size() > 0){
//            msg.setState(0);
//            msg.setResult("登录成功");
//            msg.setData(login.get(0).getNickname());
//            System.out.println(msg);
            Admin loginAdmin = login.get(0);
            model.addAttribute("admin", loginAdmin);
            System.out.println(loginAdmin);
            return "redirect:/user/list";
//            return "/admin/list";
        }
        //验证失败，返回登录页面
        msg.setState(-1);
        msg.setResult("请输入正确的账户名和密码");
        model.addAttribute("loginState", msg);
        return "/admin/login";
    }

    @RequestMapping("/update")
    public String updateUser(User user, Model model){
        Integer update = userService.update(user);
        Message msg = new Message();
        msg.setState(update - 1);
        msg.setResult(update == 1 ?"修改成功":"修改失败");
        model.addAttribute("updateState", msg);
        return "redirect:/user/list";
    }



    @RequestMapping("/commentList")
    public String commentList(){
        return "/admin/commentList";
    }



    @RequestMapping("/commentInfo")
    public String commentInfo(Integer cid, Model model){

        //获取父话题
        BootStrapComment comment =
                ToBootStrapUtil.getBootStrapComment(commentService.findByCid(cid));
        model.addAttribute("comment", comment);
        return "/admin/info";
    }
}
